from othello import *
import Console

c = Console.Console(1)

class GUIOthelloBoard(OthelloBoard):

    def draw_board(self):
        c.text(1,0,'01234567')
        for x,row in izip(count(0),self.board):
            c.text(0,x+1,str(x))
            for y,space in izip(count(0),row):
                #c.text(x, y, self.print_dict[space])
                self.draw_piece(Move(x,y,space),NORMAL)
        c.rectangle((16,0,20,2))
        c.text(10,0,'W:%d B:%d' % (self.piece_count(WHITE), self.piece_count(BLACK)))
        c.text(10,1,'Diff :%d' % (self.piece_count(WHITE) - self.piece_count(BLACK)))
    def draw_piece(self,move,bkgrd):
        if move is not None:
            c.text(move.x+1,move.y+1,print_dict[move.color],print_bkgrd[move.color][bkgrd])
    def make_flips_pretty(self,move):
        flips = self.get_flips(move)
        for move in flips:
            self.flip(move)
            #c.text(x+1,y+1,self.board.print_dict[color],0x3f)
            self.draw_piece(move,FLIP)
        time.sleep(1)


class GUIHumanPlayer(Player):
    def get_move(self,board):
        for move in board.legal_moves(self.color):
            board.draw_piece(Move(move.x,move.y,EMPTY),MOVE)
        x = y = 0
        c.pos(1,1)
        while True:
            k = c.get()
            if k.type=='KeyPress':
                if k.keysym=='Up':
                    y = max(y - 1,0)
                if k.keysym=='Down':
                    y = min(y + 1,7)
                if k.keysym=='Left':
                    x = max(x - 1,0)
                if k.keysym=='Right':
                    x = min(x + 1,7)
                c.pos(x+1,y+1)
                if k.keysym=='space' or k.keysym=='Return':
                    board.draw_board()
                    return Move(x,y,self.color) 

class GUIGame(object):
    player_dict = {WHITE:'White',BLACK:'Black'}
    def __init__(self, player1,player2):
        self.players = {}
        self.players[WHITE]= player1#(WHITE,p1strat)
        self.players[BLACK]= player2#(BLACK,p2strat)
        self.board        = GUIOthelloBoard()
        self.board.setup_board()

   
    def make_move(self,move):
        if not self.board.legal_move(move):
            raise Exception, 'Illegal Move'
        self.board.put(move)
        self.board.make_flips_pretty(move)
        self.board.player = self.board.player * -1
    
    def game_over(self):
        return self.board.game_over() 
        
    def play_game(self):
        self.board.draw_board()
        while not self.game_over():
            for player in (WHITE, BLACK):
                if(self.board.legal_moves(player)):
                    print self.players[player].color
                    print self.board.player
                    move = self.players[player].get_move(self.board.copy())
                    self.board.draw_piece(move,MOVE)
                    c.text(0,10,'%s moves to (%d, %d)' % (self.player_dict[player],move.x,move.y))
                    flips = self.board.num_flips(move)
                    c.text(0,11,'Flips: %d' % flips)                    
                    time.sleep(0.5)
                    self.make_move(move)
                    self.board.draw_board()
                else:
                    c.text(0,10,'%s passes.' % self.player_dict[player])
                    c.text(0,11,'Flips: 0')
                    time.sleep(2)
        #print 'Game Over!'
        c.text(0,15,'Game Over!')

if __name__=='__main__':
    try:
        import psyco
        psyco.full()
    except ImportError:
        print 'Psyco could not be loaded'
    #game = Game(RandomPlayer(WHITE,None),RandomPlayer(BLACK,None))
    game = GUIGame(Minimax(WHITE,dynamic_squares,2), Minimax(BLACK,dynamic_squares,3))
    game.play_game()

